package com.joyoung.toastimpl;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.joyoung.control.toast.IToastBuilder;

import java.lang.ref.WeakReference;

/**
 * ToastBuilderImpl
 *
 */
@SuppressWarnings({"WeakerAccess", "unused"})
public class ToastBuilderImpl implements IToastBuilder {

    private int mIconType;
    private String mToastId;
    private Drawable mDrawable;
    private CharSequence mContent;
    private IToastAction mInitAction;
    private IToastAction mReplaceAction;

    private boolean mCancelLast = true;

    private int mViewLayout;
    private int mDuration = Toast.LENGTH_SHORT;

    private static String sLastToastId;
    private static WeakReference<Toast> sLastReference;

    @Override
    public IToastBuilder setIcon(Drawable drawable) {
        mDrawable = drawable;
        return this;
    }

    @Override
    public IToastBuilder setIconType(int iconType) {
        mIconType = iconType;
        return this;
    }

    @Override
    public IToastBuilder setContent(CharSequence content) {
        mContent = content;
        return this;
    }

    @Override
    public IToastBuilder setDuration(int duration) {
        mDuration = duration;
        return this;
    }

    // 暂未实现
    @Override
    public IToastBuilder setGravity(int gravity) {
        return this;
    }

    @Override
    public IToastBuilder cancelLast(boolean cancelLast) {
        mCancelLast = cancelLast;
        return this;
    }

    @Override
    public IToastBuilder replace(String toastId, IToastAction replaceAction) {
        mToastId = toastId;
        mReplaceAction = replaceAction;
        return this;
    }

    @Override
    public IToastBuilder setView(@LayoutRes int layout, IToastAction initAction) {
        mViewLayout = layout;
        mInitAction = initAction;
        return this;
    }

    @Override @SuppressLint("InflateParams")
    public void show(@NonNull final Context context) {
        Toast result = null;
        if (sLastReference != null && sLastReference.get() != null) {
            if (!TextUtils.isEmpty(mToastId) && mToastId.equals(sLastToastId)) {
                result = sLastReference.get();
            } else if (mCancelLast) {
                sLastReference.get().cancel();
            }
        }
        boolean isDefaultLayout = mViewLayout <= 0;
        View view;
        if (result == null) {
            result = new Toast(context);
            if (mViewLayout <= 0) {
                // 默认布局
                isDefaultLayout = true;
                view = LayoutInflater.from(context).inflate(R.layout.control_toast, null);
            } else {
                // 指定布局
                view = LayoutInflater.from(context).inflate(mViewLayout, null);
                // 初始化
                if (mInitAction != null) {
                    mReplaceAction = mInitAction;
                }
            }
            result.setView(view);
        } else {
            view = result.getView();
        }
        if (isDefaultLayout) {
            // 默认布局替换
            mReplaceAction = new IToastAction() {
                @Override
                public void accept(View view) {
                    TextView tv = view.findViewById(R.id.control_toast_message_tv);
                    ImageView iv = view.findViewById(R.id.control_toast_icon_iv);
                    tv.setText(mContent);
                    if (mDrawable == null && mIconType <= 0) {
                        iv.setVisibility(View.GONE);
                    } else {
                        if (mDrawable == null) {
                            mDrawable = getDrawableByType(context);
                        }
                        iv.setImageDrawable(mDrawable);
                        iv.setVisibility(View.VISIBLE);
                    }
                }
            };
        }
        if (mReplaceAction != null && view != null) {
            mReplaceAction.accept(view);
        }
        result.setGravity(Gravity.CENTER, 0, 0);
        result.setDuration(mDuration);
        sLastToastId = mToastId;
        sLastReference = new WeakReference<>(result);
        result.show();
    }

    private Drawable getDrawableByType(Context context) {
        if (mIconType == ICON_TYPE_OK) {
            return context.getResources().getDrawable(R.drawable.ic_toast_ok);
        } else if (mIconType == ICON_TYPE_WARN) {
            return context.getResources().getDrawable(R.drawable.ic_toast_warn);
        } else {
            return context.getResources().getDrawable(R.drawable.ic_toast_error);
        }
    }
}
